<?php

class StockAction extends AdminbaseAction
{
    protected  $dao,$fields;
    public function _initialize()
    {
        parent::_initialize();
		$this->dao = D('Admin.'.MODULE_NAME);
		$fields = F($this->moduleid.'_Field');
		foreach($fields as $key => $res){
			$res['setup']=string2array($res['setup']);
			$this->fields[$key]=$res;
		}
		unset($fields);
		unset($res);
		$this->assign ('fields',$this->fields);
    }

    /**
	 * 列表
	 *
	 */
    public function index()
    {
		$this->assign ( 'Logistics', M('Logistics') -> select() );
		$template =  file_exists(TEMPLATE_PATH.'/'.GROUP_NAME.'/'.MODULE_NAME.'_index.html') ? MODULE_NAME.'_index' : 'Content_index';
		$this->_list(MODULE_NAME);
        $this->display ($template);
    }

    public function alert()
    {
		$this->assign ( 'Logistics', M('Logistics') -> select() );
		$template =  file_exists(TEMPLATE_PATH.'/'.GROUP_NAME.'/'.MODULE_NAME.'_index.html') ? MODULE_NAME.'_index' : 'Content_index';
		$this->_list2(MODULE_NAME);
        $this->display ($template);
    }

	public function add()
    {
		$this->assign ( 'productadmin', M('productadmin') -> select() );
		$form=new Form();
		$this->assign ( 'form', $form );
		$template =  file_exists(TEMPLATE_PATH.'/'.GROUP_NAME.'/'.MODULE_NAME.'_edit.html') ? MODULE_NAME.'_edit' : 'Content_edit';
		$this->display ( $template);
	}


	public function edit()
    {
		$this->assign ( 'Logistics', M('Logistics') -> select() );
		$this->assign ( 'Order', M('Order') -> where('status =2')-> select() );
		$id = $_REQUEST ['id'];

		
		if(MODULE_NAME=='Page'){
					$Page=D('Page');
					$p = $Page->find($id);
					if(empty($p)){
					$data['id']=$id;
					$data['title'] = $this->categorys[$id]['catname'];
					$data['keywords'] = $this->categorys[$id]['keywords'];
					
					$Page->add($data);	
					}
		}
		$vo = $this->dao->getById ( $id );
 		$form=new Form($vo);
		$this->assign ( 'vo', $vo );
		$this->assign ( 'form', $form );
		$template =  file_exists(TEMPLATE_PATH.'/'.GROUP_NAME.'/'.MODULE_NAME.'_edit.html') ? MODULE_NAME.'_edit' : 'Content_edit';
		$this->display ( $template);
	}

    /**
     * 录入
     *
     */
    public function insert()
    {
		$model = $this->dao;
		$fields = $this->fields;
		$_POST = $this->checkfield($fields,$_POST);
		

		if($fields['createtime']  && empty($_POST['createtime']) )$_POST['createtime'] = time();		 
		if($fields['updatetime'])$_POST['updatetime'] = $_POST['createtime'];	
        $_POST['userid'] = $_SESSION['userid'];
		$_POST['username'] = $_SESSION['username'];
		if($_POST['style_color']) $_POST['style_color'] = 'color:'.$_POST['style_color'];
		if($_POST['style_bold']) $_POST['style_bold'] =  ';font-weight:'.$_POST['style_bold'];
		if($_POST['style_color'] || $_POST['style_bold'] ) $_POST['title_style'] = $_POST['style_color'].$_POST['style_bold'];
 


//获取产品名称
if($mo_tmp=M('productadmin')->where("productcode='".$_POST['title']."'")->find()){
$_POST['productcode']=$_POST['title'];
$_POST['title']=$mo_tmp['title'];
}else{
$this->error('未识别的产品编号');
}
//-------------------------



//记入库存表
		$stockmodel=M('stock');

if($mo_tmp=$stockmodel->where('productcode="'.$_POST['productcode'].'"')->find()){
	$stockmodel->setInc('number','productcode="'.$_POST['productcode'].'"',(int)$_POST['number']);
}else{
		if (false === $stockmodel->create ()) {
			$this->error ( $stockmodel->getError () );
		}
		$id= $stockmodel->add();
		if ($id ===false){
			$this->error ( $stockmodel->getError () );
		}
}
//-------------------------
		
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$id= $model->add();
		if ($id !==false) {

			$catid = MODULE_NAME =='Page' ? $id : $_POST['catid'];

			if($_POST['aid']) {
				$Attachment =M('Attachment');		
				$aids =  implode(',',$_POST['aid']);
				$data['id']=$id;
				$data['catid']= $catid;
				$data['status']= '1';
				$Attachment->where("aid in (".$aids.")")->save($data);
			}

			$data='';
			$cat = $this->categorys[$catid];
			$url = geturl($cat,$id,$this->sysConfig);
			$data['id']= $id;
			$data['url']= $url[0];
			$this->dao->save($data);

			if($cat['ishtml'] && $_POST['status']){
				if(MODULE_NAME!='Page'   && $_POST['status'])	$this->create_show($id,MODULE_NAME);
				if($this->sysConfig['HOME_ISHTML']) $this->create_index();
				$arrparentid = array_filter(explode(',',$cat['arrparentid'].','.$cat['id']));
				foreach($arrparentid as $catid) {
					if($this->categorys[$catid]['ishtml'])	$this->clisthtml($catid);					
				}				
 			}
			$this->assign ( 'jumpUrl', U(MODULE_NAME.'/index') );
			$this->success (L('add_ok'));
		} else {
			$this->error (L('add_error').': '.$model->getDbError());
		}

    }

	function update()
	{  
		$model = $this->dao;
		$fields = $this->fields;
		$_POST = $this->checkfield($fields,$_POST);
 
		if($fields['updatetime']) $_POST['updatetime'] = time();		
		if($_POST['style_color']) $_POST['style_color'] = 'color:'.$_POST['style_color'];
		if($_POST['style_bold']) $_POST['style_bold'] =  ';font-weight:'.$_POST['style_bold'];
		if($_POST['style_color'] || $_POST['style_bold'] ) $_POST['title_style'] = $_POST['style_color'].$_POST['style_bold'];

		$cat = $this->categorys[$_POST['catid']];
		$_POST['url'] = geturl($cat,$_POST['id'],$this->sysConfig);
		$_POST['url'] =$_POST['url'][0];

		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		
		// 更新数据
		$list=$model->save ();
		if (false !== $list) {
			$id= $_POST['id'];
			$catid = MODULE_NAME =='Page' ? $id : $_POST['catid'];

			if($_POST['aid']) {
				$Attachment =M('Attachment');		
				$aids =  implode(',',$_POST['aid']);
				$data['id']= $id;
				$data['catid']= $catid;
				$data['status']= '1';
				$Attachment->where("aid in (".$aids.")")->save($data);
			}
			$cat = $this->categorys[$catid];
			if($cat['ishtml']){
				if(MODULE_NAME!='Page'  && $_POST['status'])	$this->create_show($_POST['id'],MODULE_NAME);				
				if($this->sysConfig['HOME_ISHTML']) $this->create_index();
				$arrparentid = array_filter(explode(',',$cat['arrparentid'].','.$cat['id']));
				foreach($arrparentid as $catid) {
					if($this->categorys[$catid]['ishtml'])	$this->clisthtml($catid);					
				}				
 			}
			$this->assign ( 'jumpUrl', $_SERVER['HTTP_REFERER'] );
			$this->success (L('edit_ok'));
		} else {
			//错误提示
			$this->success (L('edit_error').': '.$model->getDbError());
		}
	}

	function checkfield($fields,$_POST){
		foreach ( $_POST as $key => $val ) {
				$setup=$fields[$key]['setup'];

				//$setup=string2array($fields[$key]['setup']);

				if(!empty($fields[$key]['required']) && empty($_POST[$key])) $this->error (L('do_empty'));

				if($setup['multiple'] || $setup['inputtype']=='checkbox' || $fields[$key]['type']=='checkbox'){
					$_POST[$key] = implode(',',$_POST[$key]);		
				}elseif($fields[$key]['type']=='datetime'){
					$_POST[$key] =strtotime($_POST[$key]);
				}elseif($fields[$key]['type']=='textarea'){
					$_POST[$key]=addslashes($_POST[$key]);
				}elseif($fields[$key]['type']=='images' || $fields[$key]['type']=='files'){
					$name = $key.'_name';
					$arrdata =array();
					foreach($_POST[$key] as $k=>$res){
						 $arrdata[]=$_POST[$key][$k].'|'.$_POST[$name][$k];
					}
					$_POST[$key]=implode(':::',$arrdata);
				}elseif($fields[$key]['type']=='editor'){					
					//自动提取摘要
					if(isset($_POST['add_description']) && $_POST['description'] == '' && isset($_POST['content'])) {
						$content = stripslashes($_POST['content']);
						$description_length = intval($_POST['description_length']);
						$_POST['description'] = str_cut(str_replace(array("\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;'), '', strip_tags($content)),$description_length);
						$_POST['description'] = addslashes($_POST['description']);
					}
					//自动提取缩略图
					if(isset($_POST['auto_thumb']) && $_POST['thumb'] == '' && isset($_POST['content'])) {
						$content = $content ? $content : stripslashes($_POST['content']);
						$auto_thumb_no = intval($_POST['auto_thumb_no']) * 3;
						if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {
							$_POST['thumb'] = $matches[$auto_thumb_no][0];
						}
					}
				}else{
					
				}
		}
		return $_POST;
	}


	protected function _list($modelname, $map = '', $sortBy = '', $asc = false ,$listRows = 15) {
		$model = M($modelname);
		$id=$model->getPk ();
		$this->assign ( 'pkid', $id );

		if (isset ( $_REQUEST ['order'] )) {
			$order = $_REQUEST ['order'];
		} else {
			$order = ! empty ( $sortBy ) ? $sortBy : $id;
		}
		if (isset ( $_REQUEST ['sort'])) {
			$_REQUEST ['sort']=='asc' ? $sort = 'asc' : $sort = 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}


		$_REQUEST ['sort'] = $sort;
		$_REQUEST ['order'] = $order;


		$keyword=$_REQUEST['keyword'];
		$searchtype=$_REQUEST['searchtype'];
		$groupid =intval($_REQUEST['groupid']);
		$catid =intval($_REQUEST['catid']);
		$posid =intval($_REQUEST['posid']);
		$typeid =intval($_REQUEST['typeid']);


if($_REQUEST['startdate'] && $_REQUEST['enddate']){
//高级查询
		$_REQUEST['startdate']=strtotime($_REQUEST['startdate']);
		$_REQUEST['enddate']=strtotime($_REQUEST['enddate']);
		$map['createtime']=array(array('gt',$_REQUEST['startdate']),array('lt',$_REQUEST['enddate'])) ;
		if($_REQUEST['title'])$map['title']=$_REQUEST['title'];
}else{
//普通查询
		if(!empty($keyword) && !empty($searchtype)){
			$map[$searchtype]=array('like','%'.$keyword.'%');
		}
}
		if($groupid)$map['groupid']=$groupid;
		if($catid)$map['catid']=$catid;
		if($posid)$map['posid']=$posid;
		if($typeid) $map['typeid']=$typeid;
		if(isset($_REQUEST['status']) && ( $_REQUEST['status']==='0' || $_REQUEST['status']>0)){
			$map['status']=intval($_REQUEST['status']);
		}else{
			unset($_REQUEST['status']);
		}

		$this->assign($_REQUEST);

		//取得满足条件的记录总数
		$count = $model->where ( $map )->count ( $id );
//		echo $model->getLastSql();
		if ($count > 0) {
			import ( "@.ORG.Page" );
			//创建分页对象
			if (! empty ( $_REQUEST ['listRows'] )) {
				$listRows = $_REQUEST ['listRows'];
			}
			$p = new Page ( $count, $listRows );
			//分页查询数据

			$field=$this->module[$this->moduleid]['listfields'];
			$voList = $model->field($field)->where($map)->order( "`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->findAll ( );
			//分页跳转的时候保证查询条件
			foreach ( $map as $key => $val ) {
				if (! is_array ( $val )) {
					$p->parameter .= "$key=" . urlencode ( $val ) . "&";
				}
			}

			$map[C('VAR_PAGE')]='{$page}';
			$page->urlrule = U($modelname.'/index', $map);


			//分页显示
			$page = $p->show ();
			//列表排序显示
			$sortImg = $sort; //排序图标
			$sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
			$sort = $sort == 'desc' ? 1 : 0; //排序方式
if($_REQUEST['startdate'] && $_REQUEST['enddate']){
static $totalprice;
foreach($voList as $tmpval){
$totalprice+=$tmpval['price'];
}
$p=$totalprice;
$totalprice='';
$totalprice=' 总费用:'.$p.'元';
}
			//模板赋值显示


			$this->assign ( 'list', $voList );
			$this->assign ( 'page', $page );
			$this->assign ( 'totalprice', $totalprice );
		}
		return;
	}

	protected function _list2($modelname, $map = '', $sortBy = '', $asc = false ,$listRows = 15) {
		$model = M($modelname);
		$id=$model->getPk ();
		$this->assign ( 'pkid', $id );

		if (isset ( $_REQUEST ['order'] )) {
			$order = $_REQUEST ['order'];
		} else {
			$order = ! empty ( $sortBy ) ? $sortBy : $id;
		}
		if (isset ( $_REQUEST ['sort'])) {
			$_REQUEST ['sort']=='asc' ? $sort = 'asc' : $sort = 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}


		$_REQUEST ['sort'] = $sort;
		$_REQUEST ['order'] = $order;


		$keyword=$_REQUEST['keyword'];
		$searchtype=$_REQUEST['searchtype'];
		$groupid =intval($_REQUEST['groupid']);
		$catid =intval($_REQUEST['catid']);
		$posid =intval($_REQUEST['posid']);
		$typeid =intval($_REQUEST['typeid']);


if($_REQUEST['startdate'] && $_REQUEST['enddate']){
//高级查询
		$_REQUEST['startdate']=strtotime($_REQUEST['startdate']);
		$_REQUEST['enddate']=strtotime($_REQUEST['enddate']);
		$map['createtime']=array(array('gt',$_REQUEST['startdate']),array('lt',$_REQUEST['enddate'])) ;
		if($_REQUEST['title'])$map['title']=$_REQUEST['title'];
}else{
//普通查询
		if(!empty($keyword) && !empty($searchtype)){
			$map[$searchtype]=array('like','%'.$keyword.'%');
		}
}
		if($groupid)$map['groupid']=$groupid;
		if($catid)$map['catid']=$catid;
		if($posid)$map['posid']=$posid;
		if($typeid) $map['typeid']=$typeid;
		if(isset($_REQUEST['status']) && ( $_REQUEST['status']==='0' || $_REQUEST['status']>0)){
			$map['status']=intval($_REQUEST['status']);
		}else{
			unset($_REQUEST['status']);
		}

		$this->assign($_REQUEST);

		//取得满足条件的记录总数
		$count = $model->where ( $map )->count ( $id );
//		echo $model->getLastSql();
		if ($count > 0) {
			import ( "@.ORG.Page" );
			//创建分页对象
			if (! empty ( $_REQUEST ['listRows'] )) {
				$listRows = $_REQUEST ['listRows'];
			}
			$p = new Page ( $count, $listRows );
			//分页查询数据

			$field=$this->module[$this->moduleid]['listfields'];
			$voList = $model->field($field)->where($map)->order( "`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->findAll ( );
			//分页跳转的时候保证查询条件
			foreach ( $map as $key => $val ) {
				if (! is_array ( $val )) {
					$p->parameter .= "$key=" . urlencode ( $val ) . "&";
				}
			}

			$map[C('VAR_PAGE')]='{$page}';
			$page->urlrule = U($modelname.'/index', $map);


			//分页显示
			$page = $p->show ();
			//列表排序显示
			$sortImg = $sort; //排序图标
			$sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
			$sort = $sort == 'desc' ? 1 : 0; //排序方式
if($_REQUEST['startdate'] && $_REQUEST['enddate']){
static $totalprice;
foreach($voList as $tmpval){
$totalprice+=$tmpval['price'];
}
$p=$totalprice;
$totalprice='';
$totalprice=' 总费用:'.$p.'元';
}
			//模板赋值显示

foreach($voList as $k=>$val){

if($val['number']>$val['lowernumber']) unset($voList[$k]);

}

			$this->assign ( 'list', $voList );
			$this->assign ( 'page', $page );
			$this->assign ( 'totalprice', $totalprice );
		}
		return;
	}
}
